package cc.rengu.igas.channel.wechat.core.service.trans;


import cc.rengu.igas.channel.wechat.common.constant.WechatConfigConstant;
import cc.rengu.igas.channel.wechat.common.constant.WechatParamConstant;
import cc.rengu.igas.channel.wechat.common.constant.WechatServiceConstant;
import cc.rengu.igas.channel.wechat.common.constant.WechatTreeNodeConstant;
import cc.rengu.igas.channel.wechat.common.enums.RespCodeEnum;
import cc.rengu.igas.channel.wechat.core.model.ChnlBizResponse;
import cc.rengu.igas.channel.wechat.core.service.base.MchntService;
import cc.rengu.igas.channel.wechat.facade.request.WechatQueryApplymentRequest;
import cc.rengu.igas.channel.wechat.facade.response.WechatQueryApplymentResponse;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.entity.DstChannelInfo;
import cc.rengu.oltp.service.common.entity.SysParam;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.realize.SysParamService;
import cc.rengu.oltp.service.realize.impl.SysParamServiceImpl;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.StringUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.communication.HttpMethod;


/**
 * 查询申请单审核结果
 */
public class QueryApplymentService extends MchntService {

    @Override
    protected Object beforeBizProcess() throws Exception {
        WechatQueryApplymentRequest queryApplymentRequest = new WechatQueryApplymentRequest();
        ConvertUtil.convertOutput(queryApplymentRequest);
        return queryApplymentRequest;
    }

    @Override
    protected ChnlBizResponse callBizService(Object request) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        int iReturnCode = 0;

        WechatQueryApplymentResponse queryApplymentResponse = new WechatQueryApplymentResponse();
        WechatQueryApplymentRequest queryApplymentRequest = (WechatQueryApplymentRequest) request;

        DstChannelInfo dstChannelInfo = (DstChannelInfo) xmlTreeUtil.getXmlTreeObjectValue(WechatTreeNodeConstant.DST_CHANNEL_INFO, DstChannelInfo.class);
        SysParamService sysParamService = new SysParamServiceImpl();
        SysParam sysParam = sysParamService.getSysParamInfo(queryApplymentRequest.getHeader().getInstId(),
                WechatConfigConstant.WECHAT_CONF_NODE, WechatConfigConstant.WECHAT_APPLYMENT_QUERY_URL);
        if (sysParam == null) {
            rglog.error("获取微信查询申请单审核结果url失败，请配置");
            throw new BizException(RespCodeEnum.CONFIG_PARAM_NOT_EXIST.getRespCode(), RespCodeEnum.CONFIG_PARAM_NOT_EXIST.getRespDesc());
        }
        String requestUrl = sysParam.getParamValue();
        if (!StringUtil.isEmptyOrNull(queryApplymentRequest.getBusinessCode()) && !StringUtil.isEmptyOrNull(queryApplymentRequest.getApplymentId())) {
            requestUrl = requestUrl + "applyment_id=" + queryApplymentRequest.getApplymentId() + "&business_code=" + queryApplymentRequest.getBusinessCode();
        } else if (!StringUtil.isEmptyOrNull(queryApplymentRequest.getBusinessCode())) {
            requestUrl = requestUrl + "business_code=" + queryApplymentRequest.getBusinessCode();
        } else if (!StringUtil.isEmptyOrNull(queryApplymentRequest.getApplymentId())) {
            requestUrl = requestUrl + "applyment_id=" + queryApplymentRequest.getApplymentId();
        } else {
            throw new BizException(RespCodeEnum.REQ_PARAM_LESS.getRespCode(), "ApplymentId或BusinessCode缺失！");
        }
        xmlTreeUtil.setXmlTreeStringValue(WechatTreeNodeConstant.DEFINE_HTTP_METHOD_NODE, "GET");
        xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.TXN_NUM, queryApplymentRequest.getHeader().getTxnNum());
        xmlTreeUtil.setXmlTreeStringValue(WechatTreeNodeConstant.DEFINE_HTTP_URL_NODE, requestUrl);
        iReturnCode = schedule.callService(WechatServiceConstant.WECHAT_V3_CLIENT_SERVICE, HttpMethod.GET);
        if (xmlTreeUtil.isExist(WechatTreeNodeConstant.DEFINE_HTTP_URL_NODE)) {
            xmlTreeUtil.deleteXmlTreeNode(WechatTreeNodeConstant.DEFINE_HTTP_URL_NODE);
        }
        if (0 != iReturnCode && 98 != iReturnCode) {
            rglog.error("调用微信服<{}>失败，iReturnCode:<{}>", dstChannelInfo.getDstServiceName(), iReturnCode);
            throw new BizException(RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespCode(), RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespDesc());
        } else if (98 == iReturnCode) {
            rglog.error("调用微信服<{}>超时，iReturnCode:<{}>", dstChannelInfo.getDstServiceName(), iReturnCode);
            throw new BizException(RespCodeEnum.TIME_OUT_ERROR.getRespCode(), RespCodeEnum.TIME_OUT_ERROR.getRespDesc());
        }
        ConvertUtil.convertOutput(queryApplymentResponse);
        String returnCode = xmlTreeUtil.getXmlTreeStringValue(WechatTreeNodeConstant.WX_RETURN_CODE);
        String returnMsg = xmlTreeUtil.getXmlTreeStringValue(WechatTreeNodeConstant.WX_RETURN_MSG);
        if (!WechatParamConstant.STRING_SUCCESS.equals(returnCode)) {
            queryApplymentResponse.setRespCode(returnCode);
            queryApplymentResponse.setRespDesc(returnMsg);
            rglog.error("调用第三方服务失败,respCode=[{}],respDesc=[{}]", queryApplymentResponse.getRespCode(), queryApplymentResponse.getRespDesc());
        } else {
            queryApplymentResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
            queryApplymentResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        }
        ChnlBizResponse chnlBizResponse = new ChnlBizResponse();
        chnlBizResponse.setResult(queryApplymentResponse);
        return chnlBizResponse;
    }

    @Override
    protected void afterBizProcess(ChnlBizResponse response) throws Exception {
        WechatQueryApplymentResponse queryApplymentResponse = (WechatQueryApplymentResponse) response.getResult();
        ConvertUtil.convertInput(queryApplymentResponse);
    }
}
